<?php

namespace Admin\Controller;
class UserController extends BaseController {
    public function editPwd(){
        $this->display();
    }

    public function index(){
        $this->display();
    }

    public function getUserListByPage(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $pageIndex = I('post.page');
        $pageSize = I('post.rows');
        $username = I('post.username');
        if ($username==''){
            $where = '1=1';
        }else{
            $where['username'] = array('like','%'.$username.'%');
        }
        $startno = ($pageIndex - 1) * $pageSize;
        $limit = $startno .',' . $pageSize;
        $userList = D('UserView')->where($where)->order('id asc')->limit($limit)->select();
        echo json_encode($userList);
    }

    public function adminUser(){
        $this->display();
    }

    public function getAdminUserListByPage(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $pageIndex = I('post.page');
        $pageSize = I('post.rows');
        $username = I('post.username');
        if ($username==''){
            $where['id'] = array('NEQ',session('userid'));
        }else{
            $where['username'] = array('like','%'.$username.'%');
            $where['id'] = array('NEQ',session('userid'));
        }
        $count = M('admin')->where($where)->count();
        $startno = ($pageIndex - 1) * $pageSize;
        $limit = $startno .',' . $pageSize;
        $userList = M('admin')->where($where)->order('id desc')->limit($limit)->select();
        //必须将记录总数和记录分别返回
        $result = array(
            "total" => $count,   //记录总数
            "rows" => $userList  //记录
        );
        $this->ajaxReturn($result,'JSON');
    }

    public function resetPwd(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $db = M('user');
        $data = array(
            'password' => md5('a123456') //重置密码为a123456
        );
        $where['id'] = array('in',I('post.ids','intdiv'));
        if ($db->where($where)->save($data)) {
            $result = array("msg" => "操作成功");
        } else {
            $result = array("msg" => "已重置成功，请不要多次重复操作");
        }
        $this->ajaxReturn($result,'JSON');
    }

    public function resetAdminPwd(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $db = M('admin');
        $data = array(
            'password' => md5('a123456') //重置密码为a123456
        );
        $where['id'] = array('in',I('post.ids','intdiv'));
        if ($db->where($where)->save($data)) {
            $result = array("msg" => "操作成功");
        } else {
            $result = array("msg" => "已重置成功，请不要多次重复操作");
        }
        $this->ajaxReturn($result,'JSON');
    }

    public function editUserStatus(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $db = M('user');
        $lock = I('post.lock','intdiv');
        if ($lock == 1){
            $lock = 0;
        }else{
            $lock = 1;
        }
        $data = array(
            'lock' => $lock
        );
        $where['id'] = array('in',I('post.ids','intdiv'));
        if ($db->where($where)->save($data)) {
            $result = array("msg" => "操作成功", "errorCode"=> 0);
        } else {
            $result = array("msg" => "已操作成功，请不要重复多次操作","errorCode"=>1);
        }
        $this->ajaxReturn($result,'JSON');
    }
    public function editAdminUserStatus(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $db = M('admin');
        $lock = I('post.lock','intdiv');
        if ($lock == 1){
            $lock = 0;
        }else{
            $lock = 1;
        }
        $data = array(
            'lock' => $lock
        );
        $where['id'] = array('in',I('post.ids','intdiv'));
        if ($db->where($where)->save($data)) {
            $result = array("msg" => "操作成功", "errorCode"=> 0);
        } else {
            $result = array("msg" => "已操作成功，请不要重复多次操作","errorCode"=>1);
        }
        $this->ajaxReturn($result,'JSON');
    }
    /**
     * 修改密码操作
     */
    public function runEditPwd () {
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $db = M('admin');
        $old = $db->where(array('id' => session('userid')))->getField('password');
        if ($old != md5(I('post.oldpassword'))) {
            $result = array("msg" => "原始密码错误","errorCode"=>2);
            $this->ajaxReturn($result,'JSON');
        }
        if (I('post.repassword') != I('post.newpassword')) {
            $result = array("msg" => "两次密码不一致","errorCode"=>3);
            $this->ajaxReturn($result,'JSON');
        }
        $data = array(
            'id' => session('userid'),
            'password' => md5(I('post.newpassword'))
        );
        if ($db->save($data)) {
            $result = array("msg" => "操作成功","errorCode"=>0);
        } else {
            $result = array("msg" => "操作失败","errorCode"=>1);
        }
        $this->ajaxReturn($result,'JSON');
    }

    public function SaveAdminUser(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        if (I('post.password') != I('post.confirmpwd')){
            $result = array("msg" => "密码与确认密码不一致","errorCode"=>2);
            $this->ajaxReturn($result,'JSON');
        }
        $db = M('admin');
        $data = array(
            "username" => I('post.add_username'),
            "password" => md5(I('post.password'))
        );
        $id = $db->add($data);
        if ($id) {
            $result = array("msg" => "操作成功","errorCode"=>0,"id"=>$id);
        } else {
            $result = array("msg" => "操作失败","errorCode"=>1);
        }
        $this->ajaxReturn($result,'JSON');
    }

    public function checkAdminUser(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $db = M('admin');
        $adminUser = $db->where(array('username' => I('post.username')))->select();
        if($adminUser){
            $result = array("msg" => "用户名重复","isExist"=>true);
        }else{
            $result = array("msg" => "用户名可用","isExist"=>false);
        }
        $this->ajaxReturn($result,'JSON');
    }

    public function addAdminUser(){
        $this->display();
    }
}
?>